﻿<%@ WebHandler Language="C#" Class="FileSender" %>

using System;
using System.Web;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


public class FileSender : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        MyLog mylog = new MyLog(context);
        context.Response.ContentType = "text/plain";

        try
        {
            HttpServerUtility logInServerPath = context.Server;
            mylog.writeLog("FileSender get POST request");

            JToken conf = (JToken)JsonConvert.DeserializeObject(File.ReadAllText(context.Server.MapPath("../Conf/FileSenderConf.json")));
            string receiveIP = conf["receiveIP"].ToString();
            string handler = conf["receiveHandler"].ToString();

            FilePackage filePack = new FilePackage(context);
            List<string> sendingFilePaths = filePack.getZipPath();
            foreach (string filePath in sendingFilePaths)
            {
                string fileName = Path.GetFileName(filePath);
                mylog.writeLog("ready to send 【" + fileName + "】");

                using (var client = new WebClient())
                {
                    client.UploadFile(receiveIP + handler, filePath);
                }

                mylog.writeLog("finish send 【" + fileName + "】");

                System.Threading.Thread.Sleep(3000);
            }

            mylog.burn(); //日志老化

            FileHelper fileHelp = new FileHelper(context);
            fileHelp.burnSendDirectory(); //老化zip
        }
        catch (Exception ex)
        {
            mylog.writeLog(ex.Message);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}